/*
 * cppMaker.cpp
 *
 *  Created on: 2012-10-8
 *      Author: administrator
 */
#include "cppWriter.hpp"
#include "../writer.hpp"


static size_t writeHppHeader( FILE* hppFile , ProtocolModule *module ) {
	char* pWrite = workBuff ;
	ProtocolModify *modify ;
	ProtocolInclude *include ;
	pWrite += sprintf( pWrite ,
			"// 简    述：%s\n"
			"// 创建标志：%s\n"
			"// 修改标志：\n" ,
			module->summary , module->creator ) ;
	for( modify = module->modifies.next ; modify ; modify = modify->next ) {
		pWrite += sprintf( pWrite , "//		%s\n" , modify->modify ) ;
	}
	pWrite += sprintf( pWrite ,
			"\n"
			"#include <stdlib.h>\n"
			"#include <stdio.h>\n"
			"#include <stdint.h>\n"
			"#include <string.h>\n"
			"#include \"../../../Utils/MessageQueue/MsgBuf.h\"\n" ) ;
	for( include = module->includes.next ; include ; include = include->next ) {
		pWrite += sprintf( pWrite ,
				"#include \"%s.hpp\"\n" , include->file ) ;
	}
	pWrite += sprintf( pWrite ,
			"using namespace std ;\n"
			"\n"
			"\n" ) ;
//			"extern uint16_t thisModuleID ;\n\n") ;
	writeToFile( hppFile , workBuff , pWrite -workBuff ) ;
	return pWrite - workBuff ;
}

static size_t writeCppHeader( FILE* cppFile , ProtocolModule *module ) {
	size_t size ;
	size = sprintf( workBuff ,"\n"
			"#include \"%s.hpp\"\n"
			"\n"
			"\n"
			"/**\n"
			" * 模块ID\n"
			" */\n" , module->name ) ;
//			"static uint16_t thisModuleID = %d ;\n\n" ,  module->name , module->id ) ;
	writeToFile( cppFile , workBuff , size ) ;
	return size ;
}

static void writeTypes( FILE* hppFile , FILE* cppFile , ProtocolModule *module ) {
	// 解析时的链表是逆序的，现在翻转过来
	ProtocalType *curType = NULL ;
	ProtocalType *tmpType = NULL ;
	while( module->types ) {
		tmpType = module->types ;
		module->types = tmpType->next ;
		tmpType->next = curType ;
		curType = tmpType ;
	}
	module->types = curType ;
	while( curType ) {
		writeToFile( hppFile , curType->hppContent , curType->hppSize ) ;
		writeToFile( cppFile , curType->cppContent , curType->cppSize ) ;
		curType = curType->next ;
	}
}

static void writeRequesters( FILE* hppFile , FILE* cppFile , ProtocolModule *module ) {
	// 解析时的链表是逆序的，现在翻转过来
	ProtocalRequester *curRequest = NULL ;
	ProtocalRequester *tmpRequest = NULL ;
	while( module->requesters ) {
		tmpRequest = module->requesters ;
		module->requesters = tmpRequest->next ;
		tmpRequest->next = curRequest ;
		curRequest = tmpRequest ;
	}
	module->requesters = curRequest ;
	while( curRequest ) {
		writeToFile( hppFile , curRequest->hppContent , curRequest->hppSize ) ;
		writeToFile( cppFile , curRequest->cppContent , curRequest->cppSize ) ;
		free( curRequest->hppContent ) ;
		free( curRequest->cppContent ) ;
		curRequest = curRequest->next ;
	}
}

static void writeResponds( FILE* hppFile , FILE* cppFile , ProtocolModule *module ) {
	// 解析时的链表是逆序的，现在翻转过来
	ProtocalRespond *curRespond = NULL ;
	ProtocalRespond *tmpRespond = NULL ;
	while( module->responds ) {
		tmpRespond = module->responds ;
		module->responds = tmpRespond->next ;
		tmpRespond->next = curRespond ;
		curRespond = tmpRespond ;
	}
	module->responds = curRespond ;
	while( curRespond ) {
		writeToFile( hppFile , curRespond->hppContent , curRespond->hppSize ) ;
		writeToFile( cppFile , curRespond->cppContent , curRespond->cppSize ) ;
		curRespond = curRespond->next ;
	}
}

int writeCppModule( ProtocolModule *module ) {
	FILE* hppFile ;
	FILE* cppFile ;
	char hppPath[256] ;
	char cppPath[256] ;

	sprintf( hppPath , "%s%s.hpp" , cppDirPath , module->name ) ;
	sprintf( cppPath , "%s%s.cpp" , cppDirPath , module->name ) ;
	cppFile = fopen( cppPath , "wb" ) ;
	hppFile = fopen( hppPath , "wb" ) ;

	writeHppHeader( hppFile , module ) ;
	writeCppHeader( cppFile , module ) ;
	writeTypes( hppFile , cppFile , module ) ;
	writeRequesters( hppFile , cppFile , module ) ;
	writeResponds( hppFile , cppFile , module ) ;
	fclose( cppFile ) ;
	fclose( hppFile ) ;
	return 0 ;
}
